package com.junzhiit.nengji.app.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.app.service.IncomeHistoryService;
import com.junzhiit.nengji.app.web.util.Constants;
import com.junzhiit.nengji.bo.OrdersBO;
import com.junzhiit.nengji.dao.GoodsDataHistoryDAO;
import com.junzhiit.nengji.dao.IncomeHistoryDAO;
import com.junzhiit.nengji.dao.OrdersDataHistoryDAO;
import com.junzhiit.nengji.dao.RevenueSettingDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.UserConfigDAO;
import com.junzhiit.nengji.dao.UserDAO;
import com.junzhiit.nengji.model.GoodsDataHistory;
import com.junzhiit.nengji.model.IncomeHistory;
import com.junzhiit.nengji.model.OrdersDataHistory;
import com.junzhiit.nengji.model.RevenueSetting;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.User;
import com.junzhiit.nengji.model.UserConfig;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.OrdersDataHistoryStatus;
import com.junzhiit.nengji.model.enums.RevenueSettingType;

@Service
public class IncomeHistoryServiceImpl extends BaseServiceImpl implements IncomeHistoryService {

	@Autowired
	private UserDAO userDAO;
	
	@Autowired
	private RevenueSettingDAO revenueSettingDAO;
	
	@Autowired
	private IncomeHistoryDAO incomeHistoryDAO;
	
	@Autowired
	private UserConfigDAO userConfigDAO;
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private OrdersDataHistoryDAO ordersDataHistoryDAO;
	
	@Autowired
	private GoodsDataHistoryDAO goodsDataHistoryDAO;
	
	@Override
	public void addIncomeHistory(OrdersBO ordersBO,String parentId) {
		User user = userDAO.query(ordersBO.getUserId());
		Double value;
		//商家推荐用户
		if(user.getIsShop() == Is.NO.toIntValue() && StringUtils.isNoneBlank(user.getParentId()) && !user.getParentId().equals(Constants.ROOT_VALUE)){
			User parentUser = userDAO.query(user.getParentId());
			if(parentUser != null && parentUser.getIsShop() == Is.YES.toIntValue()){
				IncomeHistory incomeHistory = new IncomeHistory();
				RevenueSetting revenueSetting = revenueSettingDAO.getRevenue(RevenueSettingType.S_U.toIntValue());
				value = (ordersBO.getPrice()*ordersBO.getGoodsNumber()) * revenueSetting.getValue()/100;
				incomeHistory.setCommissionRation(revenueSetting.getValue());
				incomeHistory.setCommissionMoney(value);
				incomeHistory.setIncomeType(revenueSetting.getType());
				incomeHistory.setUserId(ordersBO.getUserId());
				incomeHistory.setIncomeShopId(parentUser.getShopId());
				incomeHistory.setTransactionMoney(ordersBO.getPayMoney());
				incomeHistory.setOrderNumber(ordersBO.getOrderNumber());
				incomeHistory.setPayTime(ordersBO.getPayTime());
				incomeHistory.setTransactionType(ordersBO.getType());
				incomeHistory.setGoodsId(ordersBO.getGoodsId());
				incomeHistory.setOrderGoodsId(ordersBO.getOrderGoodsId());
				incomeHistory.setGoodsPrice(ordersBO.getPrice());
				incomeHistory.setGoodsNumber(ordersBO.getGoodsNumber());
				incomeHistory.setEventType(ordersBO.getIsOpenRebate());
				incomeHistoryDAO.insert(incomeHistory);
				
				//商家流水
				Shop shop = shopDAO.query(parentUser.getShopId());
				if(StringUtils.isNoneBlank(shop.getId())){
					shop.setWithdrawalMoney(shop.getWithdrawalMoney()+value);
					shop.setCommendMoney(shop.getCommendMoney() + value);
					shopDAO.update(shop);//更新余额
				}
				//更新资产明细及财务报表
				OrdersDataHistory ordersDataHistory = ordersDataHistoryDAO.findByOrderId(ordersBO.getId());
				if(ordersDataHistory != null){
					ordersDataHistory.setReferrerMoney(ordersDataHistory.getReferrerMoney()+value);
					ordersDataHistoryDAO.update(ordersDataHistory);
					
					List<GoodsDataHistory> goodsDataHistoriesList = goodsDataHistoryDAO.findList(ordersDataHistory.getId());
					for(GoodsDataHistory history : goodsDataHistoriesList){
						GoodsDataHistory goodsDataHistory = goodsDataHistoryDAO.query(history.getId());
						if(goodsDataHistory != null){
							goodsDataHistory.setIsShare(Is.YES.toIntValue());
							goodsDataHistory.setShopGetBenefitRation(revenueSetting.getValue());
							goodsDataHistory.setShopGetBenefitMoney(value);
							goodsDataHistoryDAO.update(goodsDataHistory);
							
							GoodsDataHistory goodsDataHistoryParams = goodsDataHistoryDAO.query(history.getId());
							Double profit = goodsDataHistoryParams.getPrice() - goodsDataHistoryParams.getShopMoney(); //利润
							//共分出多少钱
							Double sumMoney = goodsDataHistoryParams.getReferrerMoney() +  goodsDataHistoryParams.getPrimaryBenefitMoney()+goodsDataHistoryParams.getSecondBenefitMoney()+goodsDataHistoryParams.getShopGetBenefitMoney();
							Double grossMargin = profit - sumMoney; //毛利
							Double grossMarginRate = grossMargin / profit * 100;//毛利率
							if(!grossMarginRate.isNaN()){
								goodsDataHistoryParams.setGrossMargin(grossMargin);
								goodsDataHistoryParams.setGrossMarginRate(grossMarginRate);
							}
							goodsDataHistoryDAO.update(goodsDataHistoryParams);
						}
					}
				}
			}
		}
		
		Shop shop = shopDAO.query(ordersBO.getShopId());
		if(StringUtils.isNoneBlank(shop.getUserId()) && !shop.getUserId().equals(Constants.ROOT_VALUE)){
			IncomeHistory incomeHistory = new IncomeHistory();
			User parentUser = userDAO.query(shop.getUserId());
			if(parentUser.getIsShop() == Is.YES.toIntValue()){
				RevenueSetting revenueSetting = revenueSettingDAO.getRevenue(RevenueSettingType.S_S.toIntValue());
				value = (ordersBO.getPrice()*ordersBO.getGoodsNumber()) * revenueSetting.getValue()/100;
				incomeHistory.setCommissionRation(revenueSetting.getValue());
				incomeHistory.setCommissionMoney(value);
				incomeHistory.setIncomeType(revenueSetting.getType());
				incomeHistory.setShopId(ordersBO.getShopId());
				incomeHistory.setIncomeShopId(parentUser.getShopId());
				if(StringUtils.isNoneBlank(shop.getId())){
					shop.setWithdrawalMoney(shop.getWithdrawalMoney()+value);
					shop.setCommendMoney(shop.getCommendMoney() + value);
					shopDAO.update(shop);//更新余额
				}
				
				//更新资产明细及财务报表
				OrdersDataHistory ordersDataHistory = ordersDataHistoryDAO.findByOrderId(ordersBO.getId());
				if(ordersDataHistory != null){
					ordersDataHistory.setReferrerMoney(ordersDataHistory.getReferrerMoney()+value);
					ordersDataHistoryDAO.update(ordersDataHistory);
					
					List<GoodsDataHistory> goodsDataHistoriesList = goodsDataHistoryDAO.findList(ordersDataHistory.getId());
					for(GoodsDataHistory history : goodsDataHistoriesList){
						GoodsDataHistory goodsDataHistory = goodsDataHistoryDAO.query(history.getId());
						if(goodsDataHistory != null && ordersDataHistory.getStatus() == OrdersDataHistoryStatus.CLOSE.toIntValue()){
							goodsDataHistory.setReferrerId(user.getParentId());
							goodsDataHistory.setReferrerName(parentUser.getNickName());
							goodsDataHistory.setReferrerIdentity(parentUser.getIsShop());
							goodsDataHistory.setReferrerRation(revenueSetting.getValue());
							goodsDataHistory.setReferrerMoney(value);
							goodsDataHistoryDAO.update(goodsDataHistory);
							
							GoodsDataHistory goodsDataHistoryParams = goodsDataHistoryDAO.query(history.getId());
							Double profit = goodsDataHistoryParams.getPrice() - goodsDataHistoryParams.getShopMoney(); //利润
							//共分出多少钱
							Double sumMoney = goodsDataHistoryParams.getReferrerMoney() +  goodsDataHistoryParams.getPrimaryBenefitMoney()+goodsDataHistoryParams.getSecondBenefitMoney()+goodsDataHistoryParams.getShopGetBenefitMoney();
							Double grossMargin = profit - sumMoney; //毛利
							Double grossMarginRate = grossMargin / profit * 100;//毛利率
							if(!grossMarginRate.isNaN()){
								goodsDataHistoryParams.setGrossMargin(grossMargin);
								goodsDataHistoryParams.setGrossMarginRate(grossMarginRate);
							}
							goodsDataHistoryDAO.update(goodsDataHistoryParams);
						}
					}
				}
			}else{
				RevenueSetting revenueSetting = revenueSettingDAO.getRevenue(RevenueSettingType.U_S.toIntValue());
				value = (ordersBO.getPrice()*ordersBO.getGoodsNumber()) * revenueSetting.getValue()/100;
				incomeHistory.setCommissionRation(revenueSetting.getValue());
				incomeHistory.setCommissionMoney(value);
				incomeHistory.setIncomeType(revenueSetting.getType());
				incomeHistory.setShopId(ordersBO.getShopId());
				incomeHistory.setIncomeUserId(parentUser.getId());
				
				UserConfig userConfig = userConfigDAO.getUserConfig(parentUser.getId());
				if(StringUtils.isNoneBlank(userConfig.getId())){
//					userConfig.setIncomeMoney(userConfig.getIncomeMoney() + value);
					userConfig.setPileIncomeMoney(userConfig.getPileIncomeMoney()+value);
					userConfig.setWithdrawalMoney(userConfig.getWithdrawalMoney()+value);
					userConfigDAO.update(userConfig);//更新余额
				}
				
				//更新资产明细及财务报表
				OrdersDataHistory ordersDataHistory = ordersDataHistoryDAO.findByOrderId(ordersBO.getId());
				if(ordersDataHistory != null){
					ordersDataHistory.setReferrerMoney(ordersDataHistory.getReferrerMoney()+value);
					ordersDataHistoryDAO.update(ordersDataHistory);
					
					List<GoodsDataHistory> goodsDataHistoriesList = goodsDataHistoryDAO.findList(ordersDataHistory.getId());
					for(GoodsDataHistory history : goodsDataHistoriesList){
						GoodsDataHistory goodsDataHistory = goodsDataHistoryDAO.query(history.getId());
						if(goodsDataHistory != null){
							goodsDataHistory.setReferrerId(user.getParentId());
							goodsDataHistory.setReferrerName(parentUser.getNickName());
							goodsDataHistory.setReferrerIdentity(parentUser.getIsShop());
							goodsDataHistory.setReferrerRation(revenueSetting.getValue());
							goodsDataHistory.setReferrerMoney(value);
							goodsDataHistoryDAO.update(goodsDataHistory);
							
							GoodsDataHistory goodsDataHistoryParams = goodsDataHistoryDAO.query(history.getId());
							Double profit = goodsDataHistoryParams.getPrice() - goodsDataHistoryParams.getShopMoney(); //利润
							//共分出多少钱
							Double sumMoney = goodsDataHistoryParams.getReferrerMoney() +  goodsDataHistoryParams.getPrimaryBenefitMoney()+goodsDataHistoryParams.getSecondBenefitMoney()+goodsDataHistoryParams.getShopGetBenefitMoney();
							Double grossMargin = profit - sumMoney; //毛利
							Double grossMarginRate = grossMargin / profit * 100;//毛利率
							if(!grossMarginRate.isNaN()){
								goodsDataHistoryParams.setGrossMargin(grossMargin);
								goodsDataHistoryParams.setGrossMarginRate(grossMarginRate);
							}
							goodsDataHistoryDAO.update(goodsDataHistoryParams);
						}
					}
				}
			}
			incomeHistory.setTransactionMoney(ordersBO.getPayMoney());
			incomeHistory.setOrderNumber(ordersBO.getOrderNumber());
			incomeHistory.setPayTime(ordersBO.getPayTime());
			incomeHistory.setTransactionType(ordersBO.getType());
			incomeHistory.setGoodsId(ordersBO.getGoodsId());
			incomeHistory.setOrderGoodsId(ordersBO.getOrderGoodsId());
			incomeHistory.setGoodsPrice(ordersBO.getPrice());
			incomeHistory.setGoodsNumber(ordersBO.getGoodsNumber());
			incomeHistory.setEventType(ordersBO.getIsOpenRebate());
			incomeHistoryDAO.insert(incomeHistory);
		}
		
	}

	
}
